#include <stdlib.h>
#include <iostream>
#include <verilated.h>
#include <verilated_vcd_c.h>
#include "VkeyPulse.h"
#include "VkeyPulse___024root.h"

#define MAX_TIME 2000
#define PERIOD 4
vluint64_t sim_time = 0;

// void reset(VkeyPulse*, uint64_t);
void neg_edge_eval(VkeyPulse*);
void pos_edge_eval(VkeyPulse*);

int main(int argc, char **argv) {
	VkeyPulse* dut = new VkeyPulse;

	Verilated::traceEverOn(true);
	VerilatedVcdC *m_trace = new VerilatedVcdC;
	dut->trace(m_trace, 2);
	m_trace->open("waveform.vcd");


	while(sim_time < MAX_TIME) {

		if(sim_time < int(1.5*PERIOD)) {
			dut->rstn = 0;
			dut->ikey = 1;
		} else {
			dut->rstn = 1;
		}

		if(sim_time == 5*PERIOD) {
			dut->ikey = 0;
		}

		if(sim_time == 60*PERIOD) {
			dut->ikey = 1;
		}

		if(sim_time > 100*PERIOD && sim_time < 150*PERIOD && (sim_time % 2 == 0)) {
			dut->ikey = rand() % 2;
		}

		if(sim_time == 190*PERIOD) {
			dut->ikey = 1;
		}

		if(sim_time == 200*PERIOD) {
			dut->ikey = 0;
		}

		if(sim_time == 260*PERIOD) {
			dut->ikey = 1;
		}
		
		if(sim_time % PERIOD == 0) {
			pos_edge_eval(dut);
			m_trace->dump(sim_time);
			neg_edge_eval(dut);
		}

		if((sim_time % PERIOD) - 2 == 0) {
			neg_edge_eval(dut);
			m_trace->dump(sim_time);
			pos_edge_eval(dut);
		}

		sim_time++;
	}
	dut->final();
	m_trace->close();

	delete dut;
}

void neg_edge_eval(VkeyPulse* dut) {
	dut->clk = 0;
	dut->eval();
}

void pos_edge_eval(VkeyPulse* dut) {
	dut->clk = 1;
	dut->eval();
}
